//
//  LCHomeChannelListsTableViewController+LoadData.m
//  JKFuYinProject
//
//  Created by admin on 2018/1/3.
//  Copyright © 2018年 LC. All rights reserved.
//

#import "LCHomeChannelListsTableViewController+LoadData.h"

@implementation LCHomeChannelListsTableViewController (LoadData)

- (void)loadDataArray:(NSString *)cid :(NSInteger )page {

    NSDictionary * diction = @{
                               @"interface":@"get_home_list",
                               @"id":cid,
                               @"page":[NSString stringWithFormat:@"%zd",page],
                               @"size":@"10",
                               };
    NSString * url = [NSString stringWithFormat:@"%@",http_shengjing];
    NetworkTool *networkTool = [NetworkTool sharedInstance];

    [networkTool POSTFuYinWithUrlString:url input:diction success:^(NSDictionary * dic) {
        NSString * status = [NSString stringWithFormat:@"%@",dic[@"status"]];
        if ([status isEqualToString:@"1"]) {
            NSArray *arrayData = dic[@"data"];
                if (page == 1) {
                    self.beautifulEssayListArray = [NSArray yy_modelArrayWithClass:[BeautifulEssayModel class] json:arrayData].mutableCopy;

                }else{
                    NSMutableArray *mArray = [NSArray yy_modelArrayWithClass:[BeautifulEssayModel class] json:arrayData].mutableCopy;
                    [self.beautifulEssayListArray addObjectsFromArray:mArray];
                    if (mArray == nil && self.beautifulEssayListArray.count > 1) {
                        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.41 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                            [self.tableView.mj_footer endRefreshingWithNoMoreData];
                        });
                    }
                }
            if(self.beautifulEssayListArray.count == 0){
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
            }
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.tableView reloadData];
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.41 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [self.tableView.mj_header endRefreshing];
                });
                [self.tableView.mj_footer endRefreshing];
            });
            
            NSMutableArray *cacheMutableArray = [[NSMutableArray alloc] init];
            for (NSInteger i = 0; i < self.beautifulEssayListArray.count; i++) {
                BeautifulEssayModel *model = self.beautifulEssayListArray[i];
                NSDictionary *json = [model yy_modelToJSONObject];
                [cacheMutableArray addObject:json];
            }
            dispatch_async(dispatch_get_main_queue(), ^{
                YYCache *yyCache=[YYCache cacheWithName:@"LCChannelListsCache"];
                [yyCache.diskCache setCostLimit:10*1024*1024];//磁盘最大缓存开销
                [yyCache.diskCache setAutoTrimInterval:60*60*24*2];//设置磁盘lru动态清理频率 默认 60秒
                NSString *key= [NSString stringWithFormat:@"%@_%@",@"keyChannelListsCache",cid];

                if (cacheMutableArray) {
                    [yyCache setObject:cacheMutableArray forKey:key];//添加数据到yyCache里缓存下来
                }
            });

        }else{
            NSString * msg = [NSString stringWithFormat:@"%@",dic[@"msg"]];
            dispatch_async(dispatch_get_main_queue(), ^{
                [self netErrorWithStatus:msg];
            });
        }
    
    } failure:^(NSError * error) {
        self.numberPage--;
        dispatch_async(dispatch_get_main_queue(), ^{
            [self netErrorWithStatus:NoNetwork];
        });
        [self getCacheWithCid:cid];
    }];
    
}
- (void)getCacheWithCid:(NSString *)cid{
    dispatch_async(dispatch_get_main_queue(), ^{
        YYCache *yyCache=[YYCache cacheWithName:@"LCChannelListsCache"];
        [yyCache.diskCache setCostLimit:10*1024*1024];//磁盘最大缓存开销
        [yyCache.diskCache setAutoTrimInterval:60*60*24*2];//设置磁盘lru动态清理频率 默认 60秒
        NSString *key= [NSString stringWithFormat:@"%@_%@",@"keyChannelListsCache",cid];

        if ([yyCache containsObjectForKey:key]) {
                    //根据key读取数据
            id vuale = [yyCache objectForKey:key];
            self.beautifulEssayListArray = [NSArray yy_modelArrayWithClass:[BeautifulEssayModel class] json:vuale].mutableCopy;
        }
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.tableView reloadData];
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [self.tableView.mj_header endRefreshing];
                });
                [self.tableView.mj_footer endRefreshing];
            });
    });
}

- (void)netErrorWithStatus:(NSString *)status {
    Reachability *reach = [Reachability reachabilityWithHostName:@"http://baidu.com"];
    if ([reach currentReachabilityStatus] == NotReachable){
        [SVProgressHUD showErrorWithStatus:status];
        [SVProgressHUD dismissWithDelay:0.5];
    }
}

@end
